<?php
include_once('bn_path.php');
    
    class bn_category
    {
        public $command_script;
        private $dl;
        private $mnu;
        private $gf;
        protected static $instance;
        
        public function __construct() 
        {
            $this->dl=new bn_dal();
            $this->mnu=new bn_menuctrl;
            $this->gf=new bn_genralfunc(); 
            $this->mnu->textCOL='cat';   
        }
        
    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }
        
        public function insert($cat,$desc,$pid=0)
        {
            $this->dl->insert('category',"$pid,$cat,$desc");
        }
        
        public function delete($id)
        {
          $this->dl->delete('category',$id);  
        }
        
        public function update($cat,$desc,$id) 
        {
            
         $this->dl->update('category',array('cat'=>$cat,'descr'=>$desc),array('id='=>$id));   
        }
        
        public function select_allarchiveurl()
        {
           return $this->dl->select('select concat("'.bn_Get_sitename().root.'/category.php?cid=",convert(id,char(10)))url,id,pid,cat,descr from category');
        }
        
        public function select_allarchive()
        {
           return $this->dl->select("select * from category");
        }
        
        function has_category($name,$pid)//
        {
            return $this->dl->hasrow('select cat from category where cat=? and pid=?',"$name,$pid");
        }
        
        public function menu($name='categorymnu1')
        {
           $this->mnu->name=$name;
           return $this->mnu->navmenu(self::select_allarchiveurl()->fetch());
        }
        
        public function treeview($name='categorytree1',$selected='')
        {
          $this->mnu->name=$name;
          $this->mnu->selected=$selected;
          $this->command_script=$this->mnu->command_script();
          echo $this->mnu->treemenu(self::select_allarchive()->fetch());
        }
        
        public function get_cat_id($get='cid')
        {
            if(isset($_GET[$get]))
                return $this->dl->getvalue('select id from category where id=?',$this->gf->Get_number($_GET[$get]));
        }
        
        public function __destruct() 
        {
        unset($this->dl);
        unset($this->mnuctl);    
        }
    }
?>